package cn.edu.ujn.project.supermarket.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.project.common.annotation.Log;
import cn.edu.ujn.project.common.core.controller.BaseController;
import cn.edu.ujn.project.common.core.domain.AjaxResult;
import cn.edu.ujn.project.common.core.page.TableDataInfo;
import cn.edu.ujn.project.common.enums.BusinessType;
import cn.edu.ujn.project.common.utils.poi.ExcelUtil;
import cn.edu.ujn.project.supermarket.domain.ReturnGoods;
import cn.edu.ujn.project.supermarket.service.IReturnGoodsService;

/**
 * 退货Controller
 * 
 * @author zhaomh
 * @date 2023-11-14
 */
@Controller
@RequestMapping("/system/returngoods")
public class ReturnGoodsController extends BaseController
{
    private String prefix = "system/returngoods";

    @Autowired
    private IReturnGoodsService returnGoodsService;

    @RequiresPermissions("system:returngoods:view")
    @GetMapping()
    public String returngoods()
    {
        return prefix + "/returngoods";
    }

    /**
     * 查询退货列表
     */
    @RequiresPermissions("system:returngoods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ReturnGoods returnGoods)
    {
        startPage();
        List<ReturnGoods> list = returnGoodsService.selectReturnGoodsList(returnGoods);
        return getDataTable(list);
    }

    /**
     * 导出退货列表
     */
    @RequiresPermissions("system:returngoods:export")
    @Log(title = "退货", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ReturnGoods returnGoods)
    {
        List<ReturnGoods> list = returnGoodsService.selectReturnGoodsList(returnGoods);
        ExcelUtil<ReturnGoods> util = new ExcelUtil<ReturnGoods>(ReturnGoods.class);
        return util.exportExcel(list, "退货数据");
    }

    /**
     * 新增退货
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存退货
     */
    @RequiresPermissions("system:returngoods:add")
    @Log(title = "退货", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ReturnGoods returnGoods)
    {
        return toAjax(returnGoodsService.insertReturnGoods(returnGoods));
    }

    /**
     * 修改退货
     */
    @RequiresPermissions("system:returngoods:edit")
    @GetMapping("/edit/{returnId}")
    public String edit(@PathVariable("returnId") String returnId, ModelMap mmap)
    {
        ReturnGoods returnGoods = returnGoodsService.selectReturnGoodsByReturnId(returnId);
        mmap.put("returnGoods", returnGoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存退货
     */
    @RequiresPermissions("system:returngoods:edit")
    @Log(title = "退货", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ReturnGoods returnGoods)
    {
        return toAjax(returnGoodsService.updateReturnGoods(returnGoods));
    }

    /**
     * 删除退货
     */
    @RequiresPermissions("system:returngoods:remove")
    @Log(title = "退货", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(returnGoodsService.deleteReturnGoodsByReturnIds(ids));
    }
}
